﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Models
{
    public class MLoaiTinTuc
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();
        public List<LoaiTinTuc> FindAll()
        {
            var v = from f in context.LoaiTinTucs select f;
            return v.ToList();
        }
        public List<LoaiTinTuc> FindAll(int maloai)
        {
            var v = from f in context.LoaiTinTucs.Where(d=>d.MaLoaiTinTuc==maloai) select f;
            return v.ToList();
        }
        public List<LoaiTinTuc> GetList(int start, int offset)
        {
            var v = new List<LoaiTinTuc>();
            v = FindAll().Skip(start).Take(offset).ToList();
            return v;
        }
        public List<LoaiTinTuc> FindAll_Web()
        {
            var v = from f in context.LoaiTinTucs.Where(d => d.HienThi == true) orderby f.ThuTu ascending select f;
            return v.ToList();
        }
        public int GetTotal()
        {
            return FindAll().Count();
        }
        public int GetTotal_Web()
        {
            return FindAll_Web().Count();
        }
        public string GetTenLoaiTin(int ma)
        {
            var v = from f in context.LoaiTinTucs.Where(d => d.MaLoaiTinTuc == ma) select f.TenLoaiTinTuc;
            return v.FirstOrDefault();
        }
        public void Update(LoaiTinTuc lsp)
        {
            var v = from f in context.LoaiTinTucs
                    select f;
            LoaiTinTuc lspdb = v.SingleOrDefault(d => d.MaLoaiTinTuc.Equals(lsp.MaLoaiTinTuc));
            lspdb.TenLoaiTinTuc = lsp.TenLoaiTinTuc;
            lspdb.ThuTu = lsp.ThuTu;
            context.SubmitChanges();
        }
        public void Save(LoaiTinTuc tt)
        {
            context.LoaiTinTucs.InsertOnSubmit(tt);
            context.SubmitChanges();
        }
        //public void Delete(int id)
        //{
        //    var v = from p in context.LoaiTinTucs
        //            where p.MaLoaiTinTuc.Equals(id)
        //            select p;
        //    LoaiTinTuc spdb = v.SingleOrDefault();
        //    context.LoaiTinTucs.DeleteOnSubmit(spdb);
        //    context.SubmitChanges();
        //}
        public void SaveChange()
        {
        }
        //public void HienThi(int id)
        //{
        //    var v = from f in context.LoaiTinTucs
        //            select f;
        //    LoaiTinTuc lspdb = v.SingleOrDefault(d => d.MaLoaiTinTuc.Equals(id));
        //    if (lspdb.HienThi == true)
        //    {
        //        lspdb.HienThi = false;
        //    }
        //    else lspdb.HienThi = true;
        //    context.SubmitChanges();
        //}
        public LoaiTinTuc GetByID(int id)
        {
            return (FindAll().Where(d => d.MaLoaiTinTuc == id)).SingleOrDefault();
        }


        public int GetMax()
        {
            return (from t in context.LoaiTinTucs
                    orderby t.MaLoaiTinTuc descending
                    select t.MaLoaiTinTuc).FirstOrDefault();
        }
        public int? GetMaxTT()
        {
            return (from t in context.LoaiTinTucs
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }
        public string Delete(int ma)
        {
            try
            {

                var v = from p in context.LoaiTinTucs
                        where p.MaLoaiTinTuc.Equals(ma)
                        select p;
                LoaiTinTuc spdb = v.SingleOrDefault();
                context.LoaiTinTucs.DeleteOnSubmit(spdb);
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public string HienThi(int id)
        {
            try
            {
                var v = from f in context.LoaiTinTucs
                        select f;
                LoaiTinTuc lspdb = v.SingleOrDefault(d => d.MaLoaiTinTuc.Equals(id));
                if (lspdb.HienThi == true)
                {
                    lspdb.HienThi = false;
                }
                else lspdb.HienThi = true;
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
      

    }
}